package service

import (
	"context"
	friends "git.huoys.com/middle-business/proto/friends"
	"git.huoys.com/middle-end/kratos/pkg/log"
	pb "gitee.com/rzttw/http/api"
	grpcpb "gitee.com/rzttw/grpc/api"
	"github.com/google/wire"
	"google.golang.org/grpc"
)

var Provider = wire.NewSet(New, wire.Bind(new(pb.HttpServer),new(*Service)) )

// Service service.
type Service struct {
}

// New new a service and return.
func New() (s *Service, cf func(), err error) {
	dial, err := grpc.Dial("friends-service.sa:9000", grpc.WithInsecure())
	if err != nil {
		log.Error("dial friends-service.sa:9000  fail, msg %s", err.Error())
		return nil, nil, err
	}
	friendsClient := friends.NewFriendsClient(dial)
	resp, err := friendsClient.LoadRecommendedGuys(context.Background(), &friends.LoadOpReq{PlayerId: 10000})
	if err != nil {
		log.Error(err.Error())
		return nil, nil, err
	}
	log.Info("resp : %v", resp.Infos)
	s = &Service{
	}
	cf = s.Close
	log.Info("new service success ...")
	return
}

// SayHelloURL bm demo func.
func (s *Service) SayHelloURL(ctx context.Context, req *pb.HelloReq) (reply *pb.HelloResp, err error) {
	reply = &pb.HelloResp{}
	dial, err := grpc.Dial(req.Target, grpc.WithInsecure())
	if err != nil {
		log.Error(err.Error())
	}
	client := grpcpb.NewDemoClient(dial)
	hello, err := client.SayHello(ctx, &grpcpb.HelloReq{})
	if err != nil {
		log.Error("SayHello fail, msg %s", err.Error())
		return
	}
	reply.Content = hello.Content
	return
}


// Close close the resource.
func (s *Service) Close() {
}
